
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;

namespace Articles
{
	public class ArticleAssociationBizRules : Syrinx.BaseBizRules
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="app"></param>
		/// <param name="context"></param>
		/// <param name="info">
		///  valid info fields articleId,relatedItemId,associationName
		/// </param>
		public void createArticleAssociation(IApplication app, IRuleContext context, INameAddressSupport info)
		{
			try
			{
				if (info != null)
				{
					info.put("companyId", ActiveCompanyId);
					if (Cmn.IsEmpty(info.get("associationName")))
						info.put("associationName", "eComProd");
					app.appData.put("DA.Articles.AddArticleAssociation", info);
				}
			}
			catch (Exception e)
			{
				throw new ArticlesException("error executing createArticleAssociation.", e);
			}
		}

		public IBizObj checkForAssociatedArticleCreateIfNeeded(IApplication app, IRuleContext context, string associationName, Swaf.BGuid.Guid associatedId,
			string newArticleTitle, string newArticleUniqueTitle, string parentArticleUniqueName)
		{
			FlexiMap m = new FlexiMap();
			m.put("companyId", ActiveCompanyId);
			m.put("associationName", associationName);
			m.put("relatedItemId", associatedId);
			m.put("details", DetailLevel.idAndTitle);
			m.put("treeName", "Articles");
			IBizObjList articles = app.appData.get("DA.Articles.GetAssociatedArticleDetails", m) as IBizObjList;
			IBizObj article = null;
			if (articles == null)
				throw new ArticlesException("Cannot properly execute dar");
			if (articles.numElements == 0)
			{
				article = app.bizObjMgr.create("Article");
				article.put("id", new Swaf.BGuid.Guid());
				article.put("baseTitle", newArticleTitle);
				article.put("uniqueTitle", newArticleUniqueTitle);

				if (parentArticleUniqueName != null)
				{
					m.put("uniqueTitle", parentArticleUniqueName);
					IBizObj parentArticle = app.appData.get("DA.Articles.GetArticleIdByUniqueTitle", m) as IBizObj;
					article.put("parentId", parentArticle.get("id"));
				}
				IResults rc = app.ruleMgr.execute(context, "updateArticle", article);

				m.put("articleId", article.get("id"));
				app.appData.put("DA.Articles.AddArticleAssociation", m);
			}
			else
				article = articles[0];
			return article;
		}
		
		public IBizObjList getArticlesAssociatedWithId(IApplication app, IRuleContext context, string associationName, Swaf.BGuid.Guid associatedId)
		{
			FlexiMap m = new FlexiMap();
			m.put("relatedItemId", associatedId);
			m.put("associationName", associationName);
			IBizObjList articles = app.appData.get("DA.Articles.GetArticlesAssociateWithIdName", m) as IBizObjList;
			return articles;
		}

		public IBizObjList getAllAssociationsToArticle(IApplication app, IRuleContext context, Swaf.BGuid.Guid articleId)
		{
			IBizObjList articles = null;
			return articles;
		}

		public Swaf.BGuid.Guid getRootAssociatedArticleIdOld(IApplication app, IRuleContext context, string associationName, Swaf.BGuid.Guid associatedId, bool createdIfNeeded)
		{
			Swaf.BGuid.Guid assId = null;

			string uniqueTitle = new StringBuilder(100).Append(associationName).Append(associatedId).ToString();
			IResults rc = context.RuleGroup.execute(context, "getArticleIdFromUniqueTitle", uniqueTitle, null);
			assId = rc.CallResults as Swaf.BGuid.Guid;
			if (assId == null && createdIfNeeded)
			{

				Swaf.BGuid.Guid rootAssocPid = null;
				string rootAssociatedUniqueTitle = new StringBuilder(100).Append("SysRootCore_").Append(associationName).ToString();
				IResults rc2 = context.RuleGroup.execute(context, "getArticleIdFromUniqueTitle", rootAssociatedUniqueTitle, null);
				rootAssocPid = rc2.CallResults as Swaf.BGuid.Guid;
				if (rootAssocPid == null)
				{
					IBizObj rootArt = createNewArticleFromTitles(app, context, associationName, rootAssociatedUniqueTitle, "Articles");
					rootAssocPid = rootArt.get("id") as Swaf.BGuid.Guid;
				}
				if (rootAssocPid != null)
				{
					IBizObj parentArt = createNewArticleFromTitles(app, context,
						new StringBuilder(100).Append(associationName).Append(" ").Append(associatedId).ToString(),
						uniqueTitle, rootAssocPid, "Articles");
					assId = parentArt.get("id") as Swaf.BGuid.Guid;
				}
			}
			return assId;
		}

		public Swaf.BGuid.Guid getRootAssociatedArticleId(IApplication app, IRuleContext context, string associationName, bool createdIfNeeded, string treeName)
		{
			Swaf.BGuid.Guid assId = null;

			string[] assIds = associationName.Split(',');
			int pos = assIds.Length-1;
			for (; pos >= 0; pos--)
			{
				IResults rc = context.RuleGroup.execute(context, "getArticleIdFromUniqueTitle", assIds[pos], treeName);
				if ((assId = rc.CallResults as Swaf.BGuid.Guid) != null)
					break;
			}
			if (pos != assIds.Length - 1)
				if (createdIfNeeded)
					for (pos++; pos < assIds.Length; pos++)
					{
						IBizObj rootArt = createNewArticleFromTitles(app, context, assIds[pos], assIds[pos], assId, treeName);
						assId = rootArt.get("id") as Swaf.BGuid.Guid;
					}
				else
					assId = null;

			return assId;
		}

		protected virtual IBizObj createNewArticleFromTitles(IApplication app, IRuleContext context, string baseTitle, string uniqueTitle, string treeName)
		{
			return createNewArticleFromTitles(app, context, baseTitle, uniqueTitle, null, treeName);
		}
		protected virtual IBizObj createNewArticleFromTitles(IApplication app, IRuleContext context, string baseTitle, string uniqueTitle, Swaf.BGuid.Guid parentId, string treeName)
		{
			IBizObj obj = App.bizObjMgr.create("Article");
			obj.put("parentId", parentId);
			obj.put("isSystem", false);
			obj.put("id", new Swaf.BGuid.Guid());
			obj.put("baseTitle", baseTitle);
			obj.put("uniqueTitle", uniqueTitle);
			obj.put("treeName", treeName);
			IResults rc = App.ruleMgr.execute("updateArticle", obj);
			return obj;
		}


		public void addAssociationToArticle(IApplication app, IRuleContext context, string associationName, Swaf.BGuid.Guid associatedId, Swaf.BGuid.Guid articleId)
		{
			FlexiMap m = new FlexiMap();
			m.put("articleId", articleId);
			m.put("relatedItemId", associatedId);
			m.put("associationName", associationName);
			app.appData.put("DA.ArticlesAdmin.InsertAssociationToArticle", m);
		}

		public void removeAssociationToArticle(IApplication app, IRuleContext context, string associationName, Swaf.BGuid.Guid associatedId, Swaf.BGuid.Guid articleId)
		{
		}

		public void removeAllAssociationsToArticle(IApplication app, IRuleContext context, Swaf.BGuid.Guid articleId)
		{
		}
	}
}
